package tw.com.hitevision.whiteboard.android.options;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import tw.com.hitevision.whiteboard.android.R;

public class OptionsRecyclerViewAdapter extends ListAdapter<String, RecyclerView.ViewHolder> {

    private String TAG = this.getClass().getSimpleName();

    private List<String> optionsList;
    private OptionsRecyclerViewItemListener listener;
    private View.OnTouchListener onToggleInputListener;


    private static final DiffUtil.ItemCallback<String> diffCallback = new DiffUtil.ItemCallback<String>() {
        @Override
        public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
            return false;
        }

        @Override
        public boolean areContentsTheSame(@NonNull String oldItem, @NonNull String newItem) {
            return oldItem.equals(newItem);
        }

        @Override
        public Object getChangePayload(@NonNull String oldItem, @NonNull String newItem) {
            return newItem;
        }
    };

    public OptionsRecyclerViewAdapter(List<String> optionsList, OptionsRecyclerViewItemListener listener, View.OnTouchListener onToggleInputListener) {
        super(diffCallback);
        this.optionsList = optionsList;
        this.listener = listener;
        this.onToggleInputListener = onToggleInputListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.options_recycler_view_item_layout);
            imageView = itemView.findViewById(R.id.options_recycler_view_image);
            nextView = itemView.findViewById(R.id.options_recycler_view_next);
            textView = itemView.findViewById(R.id.options_recycler_view_text);
            context = itemView.getContext();
        }
        private Context context;
        private View view;
        private ImageView imageView;
        private ImageView nextView;
        private TextView textView;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        return new ViewHolder(inflater.inflate(R.layout.options_recycler_view_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder)holder).view.setOnClickListener(view -> listener.OnItemClick(optionsList.get(position)));
        if (optionsList.get(position).equals(((ViewHolder) holder).context.getString(R.string.plugin_app_rulers))) {
            ((ViewHolder) holder).imageView.setBackgroundResource(R.drawable.ic_plugins_rulers);
            ((ViewHolder) holder).nextView.setVisibility(View.VISIBLE);
            ((ViewHolder) holder).textView  .setText(R.string.plugin_app_rulers);
        }

    }

    @Override
    public int getItemCount() {
        return optionsList.size();
    }

    public interface OptionsRecyclerViewItemListener {
        void OnItemClick(String options);
    }
}
